﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nature.Toolkit.GMix
{
    public class MixCycleReferenceExceptioncs : System.Exception
    {
        private List<ComponentInfo> m_cycle;

        internal MixCycleReferenceExceptioncs(Stack<ComponentInfo> stack)
        {
            m_cycle = new List<ComponentInfo>(stack);
            ComponentInfo first = m_cycle.First(i => i == m_cycle.Last());
            ComponentInfo last = m_cycle.Last();
            m_cycle = m_cycle.GetRange(m_cycle.IndexOf(first), m_cycle.IndexOf(last));
        }

        internal ComponentInfo[] CycledSequence
        {
            get 
            {
                return m_cycle.ToArray();
            }
        }

        public string Display(string delimiter)
        {
            StringBuilder builder = new StringBuilder(m_cycle.First().DisplayName);
            foreach (IComponent node in m_cycle.Skip(1))
            {
                builder.AppendFormat("{0}{1}", delimiter, node.DisplayName);
            }
            return builder.ToString();
        }
    }
}
